<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="default" @click="handlePrint()" v-auth="'wms:rd:order:rdOrder:edit'">
          <Icon icon="ant-design:printer-twotone" /> {{ t('打印') }}
        </a-button>
        <Popconfirm placement="bottom" :title="t('是否确认拣货完成？')" @confirm="handleBatchOver">
          <a-button type="default" v-auth="'wms:jh:pick:jhPickMain:edit'">
            <Icon icon="ant-design:check-outlined" /> {{ t('拣货完成') }}
          </a-button>
        </Popconfirm>
        <!-- <a-button type="success" ghost @click="handleForm({})" v-auth="'wh:down:whDownTaskMain:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno, isView: true })">
          {{ record.djno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputView @register="registerViewDrawer" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsWhDownWhDownTaskMainList">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { Popconfirm } from 'ant-design-vue';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { whDownTaskMainDelete, whDownTaskMainListData, whDownTaskMainBatchOverr, updatePrintCount } from '/@/api/wms/wh/down/whDownTaskMain';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import InputView from './formView.vue';
  import FormImport from './formImport.vue';
  import PrintModal from '/@/components/print/printModal.vue';
  import { companyTreeData } from '/@/api/sys/company';
  import { useUserStore } from '/@/store/modules/user';

  const { t } = useI18n('wh.down.whDownTaskMain');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const userStore = useUserStore();

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('下架任务管理'),
  };

  onMounted(async () => {
    setProps();
    handleCompany();
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'),
        field: 'companyCode',
        fieldLabel: 'companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('单据日期'),
        field: 'date',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('拣货方式'),
        field: 'type',
        component: 'Select',
        componentProps: {
          dictType: 'jh_type',
          allowClear: true,
        },
      },
      {
        label: t('单据号'),
        field: 'djno',
        component: 'Input',
      },
      {
        label: t('来源单号'),
        field: 'sourceNo',
        component: 'Input',
      },
      {
        label: t('拣货状态'),
        field: 'jhStatus',
        component: 'Select',
        componentProps: {
          dictType: 'jh_main_status',
          allowClear: true,
        },
      },
      {
        label: t('创建日期起'),
        field: 'createDate_gte',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('创建日期止'),
        field: 'createDate_lte',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('创建人名称'),
        field: 'createByName',
        component: 'Input',
      },
      {
        label: t('总数'),
        field: 'sumQty',
        component: 'Input',
      },
      {
        label: t('累计拣货数'),
        field: 'sumPickQty',
        component: 'Input',
      },
      {
        label: t('打印次数'),
        field: 'printCount',
        component: 'Input',
      },
      {
        label: t('任务类型'),
        field: 'taskType',
        component: 'Select',
        componentProps: {
          dictType: 'wh_rd02_bus_type',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('公司名称'),
      dataIndex: 'companyName',
      key: 'a.company_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单据号'),
      dataIndex: 'djno',
      align: 'left',
      key: 'a.djno',
      sorter: true,
      width: 130,
      slot: 'firstColumn',
    },
    {
      title: t('来源单号'),
      dataIndex: 'sourceNo',
      align: 'left',
      key: 'a.source_no',
      sorter: true,
      width: 130,
    },
    {
      title: t('任务类型'),
      dataIndex: 'taskType',
      key: 'a.task_type',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'wh_rd02_bus_type',
    },
    {
      title: t('单据日期'),
      dataIndex: 'date',
      key: 'a.date',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('拣货方式'),
      dataIndex: 'type',
      key: 'a.type',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'jh_type',
    },
    {
      title: t('拣货状态'),
      dataIndex: 'jhStatus',
      key: 'a.jh_status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'jh_main_status',
    },
    {
      title: t('创建日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('创建人名称'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('公司编码'),
    //   dataIndex: 'companyCode',
    //   key: 'a.company_code',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },

    {
      title: t('总数'),
      dataIndex: 'sumQty',
      key: 'a.sum_qty',
      sorter: true,
      width: 130,
      align: 'right',
      showSummary: true,
    },
    {
      title: t('累计拣货数'),
      dataIndex: 'sumPickQty',
      key: 'a.sum_pick_qty',
      sorter: true,
      width: 130,
      align: 'right',
      showSummary: true,
    },
    {
      title: t('打印次数'),
      dataIndex: 'printCount',
      key: 'a.print_count',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 80,
    actions: (record: Recordable) => [
      // {
      //   icon: 'i-clarity:note-edit-line',
      //   title: t('编辑下架任务'),
      //   onClick: handleForm.bind(this, { djno: record.djno }),
      //   auth: 'wh:down:whDownTaskMain:edit',
      //   ifShow: () => record.jhStatus == '0',
      // },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除下架任务'),
        popConfirm: {
          title: t('是否确认删除下架任务'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wh:down:whDownTaskMain:edit',
        ifShow: () => record.jhStatus == '0',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerViewDrawer, { openDrawer: openViewDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys, setProps }] = useTable({
    api: whDownTaskMainListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
    showCustomSummary: true,
  });

  function handleForm(record: Recordable) {
    if (record.isView) {
      openViewDrawer(true, record);
    } else {
      openDrawer(true, record);
    }
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/down/whDownTaskMain/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const params = { djno: record.djno };
    const res = await whDownTaskMainDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  async function handleBatchOver() {
    if (getSelectRowKeys().length) {
      const res = await whDownTaskMainBatchOverr({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择一条数据');
    }
  }
  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  async function handlePrint() {
    let arr = await getSelectRowKeys();
    if (getSelectRowKeys().length == 0) {
      showMessage(t('请先选择数据'));
      return;
    }
    // let newArr = arr.map(item=>{
    //   return item.djno
    // })

    const res = await updatePrintCount({ selIds: arr.join(',') });
    console.log(res, 'res');
    if (res.result == 'false') {
      showMessage(res.message);
    }

    let params = {
      ids: arr.join(','),
      title: '下架任务拣货单',
      height: '0.9',
      width: '0.9',
      fileName: '下架任务拣货单',
    };
    openPrintModal(true, params);
  }

  async function handleCompany() {
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
    });
  }

</script>
